package com.sentiment.display;

import org.apache.log4j.Logger;
import org.bson.Document;
import org.json.JSONObject;

import com.sentiment.config.Config;
import com.sentiment.db.CrawlingPageDB;
import com.sentiment.db.SimilarityDB;
import com.sentiment.db.TextDB;
import com.sentiment.obj.CrawlingPage;
import com.sentiment.obj.Similarity;
import com.sentiment.obj.Text;

public class ResultDetail {
	
	private JSONObject json;
	
	public void loadingDataAndsetJSONObj(String srcId,int paraNum,int stnNum,int jumpNum ){
		Logger logger = Config.getLogger(this.getClass());
		CrawlingPageDB cpdb = new CrawlingPageDB();
		TextDB tdb = new TextDB();
		SimilarityDB sdb = new SimilarityDB();
		
		Similarity sim = sdb.findDataByKeyAndVal("srcId", srcId);
		if(sim == null){
			logger.info("[错误]从数据库SimilarityDB读取"+srcId+"失败");
			return;
		}
		Document detail = sim.getDetail();
		Document record = (Document)((Document)detail.get(String.valueOf(paraNum))).get(String.valueOf(stnNum));
		int recordAmount = record.getInteger("recordAmount");
		if(jumpNum > recordAmount){
			logger.info("[错误]传输了一个不存在的跳转条目-----"+jumpNum);
			return ;
		}
		//获得到了指定jumpNum的一条重复率记录信息
		Document entry = (Document)record.get(String.valueOf(jumpNum));
		//爬取的某篇文章的标题　通过它查找到爬取的Text和CrawlingPage
		String id = entry.getString("id");
		CrawlingPage cp = cpdb.findDataByKeyAndVal("id",id);
		Text tx = tdb.findDataByKeyAndVal("id", id);
		setJsonObj(cp,tx,entry.getInteger("paraNum"),entry.getInteger("stnNum"),entry.getString("similarity"));
	}
	
	private void setJsonObj(CrawlingPage cp,Text tx,int paraNum,int stnNum,String similarity){
		//相似句　相似度　相似内容地址　标题　相似句所在段落内容　来源（未完成　爬取时间　页面时间　相似文章总字数等
		json = new JSONObject();
		json.put("sentence", tx.getContent(paraNum, stnNum));
		json.put("similarity", similarity);
		json.put("url", cp.getUrl());
		json.put("title", tx.getTitle());
		json.put("paragraph", tx.getContent(paraNum));
		json.put("crawlingTime", cp.getCrawlingTime());
		json.put("publishTime",tx.getPublishTime());
		json.put("charsAmount", tx.getCharsAmount());
	}
	public JSONObject getJson(){
		return json;
	}
}
